package com.etboot.modules.activiti.serviceimpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etboot.modules.activiti.dao.mapper.ActStarterMapper;
import com.etboot.modules.activiti.entity.ActStarter;
import com.etboot.modules.activiti.service.ActStarterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 流程可发起人接口实现
 * @author xboot
 */
@Slf4j
@Service
@Transactional
public class ActStarterServiceImpl extends ServiceImpl<ActStarterMapper,ActStarter> implements ActStarterService {
    @Autowired
    private ActStarterMapper actStarterMapper;

    @Override
    public Set<String> findByUserId(String userId) {
        HashSet<String> starters = new HashSet<>();
        List<ActStarter> list =  actStarterMapper.selectList(Wrappers.<ActStarter>lambdaQuery().eq(ActStarter::getUserId,userId));
        for (ActStarter actStarter : list){
            starters.add(actStarter.getProcessDefId());
        }
        return starters;
    }

    @Override
    public Boolean hasRecord(String processDefId, String userId) {
        List<ActStarter> listNode = actStarterMapper.selectList(Wrappers.<ActStarter>lambdaQuery().eq(ActStarter::getProcessDefId,processDefId).eq(ActStarter::getUserId,userId));
        return listNode != null && listNode.size() > 0;
    }

    @Override
    public void deleteByProcessDefId(String processDefId) {
        actStarterMapper.delete(Wrappers.<ActStarter>lambdaQuery().eq(ActStarter::getProcessDefId,processDefId));
    }
}